﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;

using WHC.Hotel.UI.Controls;

namespace WHC.Hotel.UI
{
    public partial class FrmAddPurchaseGoods : BaseForm
    {
        public string WareHourse = "";
        public string HandNumber = "";
        public Dictionary<string, PurchaseDetailInfo> detailDict = new Dictionary<string, PurchaseDetailInfo>();
        private decimal amountMoney = 0.0M;

        public FrmAddPurchaseGoods()
        {
            InitializeComponent();
        }

        private void FrmAddConsume_Load(object sender, EventArgs e)
        {
            this.txtName.Focus();
            this.lblWareHouse.Text = WareHourse;
            this.groupConsumeList.Text = string.Format("{0} {1}", WareHourse, this.groupConsumeList.Text);

            ShowGoodListView();
            ShowGoodsTreeView();
            BindData();
        }

        private void BindData()
        {
            amountMoney = OnShowStatus(HandNumber);
            this.lblAmount.Text = string.Format("清单总金额：{0:C}", amountMoney);
        }

        private decimal OnShowStatus(string handNo)
        {
            decimal allMoney = 0.0M;

            #region 更新消费记录
            if (!string.IsNullOrEmpty(handNo))
            {
                this.lvwDetail.Items.Clear();
                int i = 1;
                foreach (PurchaseDetailInfo info in detailDict.Values)
                {
                    ListViewItem item = new ListViewItem(info.ItemNo);
                    item.SubItems.Add(info.ItemName);
                    item.SubItems.Add(info.Unit);
                    item.SubItems.Add(info.Price.ToString("C2"));
                    item.SubItems.Add(info.Quantity.ToString());
                    item.SubItems.Add(info.Amount.ToString("C2"));
                    item.Tag = info;

                    this.lvwDetail.Items.Add(item);
                    allMoney += info.Amount;
                    i++;
                }
            }
            #endregion

            this.amountMoney = allMoney;
            return allMoney;
        }

        private void ShowGoodListView()
        {
            this.lvwGoods.Items.Clear();

            #region 项目信息列表

            if (WareHourse == "客房仓库")
            {
                List<GoodsInfo> goodsList = new List<GoodsInfo>();
                if (this.txtName.Text.Length > 0)
                {
                    goodsList = BLLFactory<Goods>.Instance.FindByName(this.txtName.Text);
                }
                else
                {
                    goodsList = BLLFactory<Goods>.Instance.GetAll();
                }

                foreach (GoodsInfo info in goodsList)
                {
                    ListViewItem item = new ListViewItem(info.ItemNo);
                    item.SubItems.Add(info.Name);
                    item.SubItems.Add(info.UnitCost.ToString("C2"));
                    item.SubItems.Add(info.StockQuantity.ToString());

                    item.Tag = info;
                    this.lvwGoods.Items.Add(item);
                }
            }
            else if (WareHourse == "KTV仓库")
            {
                List<KTVGoodsInfo> goodsList = new List<KTVGoodsInfo>();
                if (this.txtName.Text.Length > 0)
                {
                    goodsList = BLLFactory<KTVGoods>.Instance.FindByName(this.txtName.Text);
                }
                else
                {
                    goodsList = BLLFactory<KTVGoods>.Instance.GetAll();
                }

                foreach (KTVGoodsInfo info in goodsList)
                {
                    ListViewItem item = new ListViewItem(info.ItemNo);
                    item.SubItems.Add(info.Name);
                    item.SubItems.Add(info.UnitCost.ToString("C2"));
                    item.SubItems.Add(info.StockQuantity.ToString());

                    item.Tag = info;
                    this.lvwGoods.Items.Add(item);
                }
            }
            else if (WareHourse == "茶室仓库")
            {
                List<TeaGoodsInfo> goodsList = new List<TeaGoodsInfo>();
                if (this.txtName.Text.Length > 0)
                {
                    goodsList = BLLFactory<TeaGoods>.Instance.FindByName(this.txtName.Text);
                }
                else
                {
                    goodsList = BLLFactory<TeaGoods>.Instance.GetAll();
                }

                foreach (TeaGoodsInfo info in goodsList)
                {
                    ListViewItem item = new ListViewItem(info.ItemNo);
                    item.SubItems.Add(info.Name);
                    item.SubItems.Add(info.UnitCost.ToString("C2"));
                    item.SubItems.Add(info.StockQuantity.ToString());

                    item.Tag = info;
                    this.lvwGoods.Items.Add(item);
                }
            }

            #endregion
        }

        private void ShowGoodsTreeView()
        {
            this.treeGoods.Nodes.Clear();
            this.treeGoods.BeginUpdate();

            #region 项目类型信息明细类别

            if (WareHourse == "客房仓库")
            {
                List<GoodsTypeInfo> typeList = BLLFactory<GoodsType>.Instance.GetAll();
                foreach (GoodsTypeInfo info in typeList)
                {
                    TreeNode typeNode = new TreeNode(info.GoodsType, 0, 0);
                    this.treeGoods.Nodes.Add(typeNode);
                    //typeNode.Tag = info;

                    List<GoodsInfo> goods = BLLFactory<Goods>.Instance.FindByGoodsType(info.GoodsType);
                    foreach (GoodsInfo goodInfo in goods)
                    {
                        string display = string.Format("{0}   {1}", goodInfo.Name, goodInfo.UnitCost);
                        TreeNode subNode = new TreeNode(display, 1, 1);
                        subNode.Tag = goodInfo;
                        typeNode.Nodes.Add(subNode);
                    }
                }
            }
            else if (WareHourse == "KTV仓库")
            {
                List<KTVGoodsTypeInfo> typeList = BLLFactory<KTVGoodsType>.Instance.GetAll();
                foreach (KTVGoodsTypeInfo info in typeList)
                {
                    TreeNode typeNode = new TreeNode(info.GoodsType, 0, 0);
                    this.treeGoods.Nodes.Add(typeNode);
                    //typeNode.Tag = info;

                    List<KTVGoodsInfo> goods = BLLFactory<KTVGoods>.Instance.FindByGoodsType(info.GoodsType);
                    foreach (KTVGoodsInfo goodInfo in goods)
                    {
                        string display = string.Format("{0}   {1}", goodInfo.Name, goodInfo.UnitCost);
                        TreeNode subNode = new TreeNode(display, 1, 1);
                        subNode.Tag = goodInfo;
                        typeNode.Nodes.Add(subNode);
                    }
                }
            }
            else if (WareHourse == "茶室仓库")
            {
                List<TeaGoodsTypeInfo> typeList = BLLFactory<TeaGoodsType>.Instance.GetAll();
                foreach (TeaGoodsTypeInfo info in typeList)
                {
                    TreeNode typeNode = new TreeNode(info.GoodsType, 0, 0);
                    this.treeGoods.Nodes.Add(typeNode);
                    //typeNode.Tag = info;

                    List<TeaGoodsInfo> goods = BLLFactory<TeaGoods>.Instance.FindByGoodsType(info.GoodsType);
                    foreach (TeaGoodsInfo goodInfo in goods)
                    {
                        string display = string.Format("{0}   {1}", goodInfo.Name, goodInfo.UnitCost);
                        TreeNode subNode = new TreeNode(display, 1, 1);
                        subNode.Tag = goodInfo;
                        typeNode.Nodes.Add(subNode);
                    }
                }
            }
            #endregion

            this.treeGoods.EndUpdate();
            this.treeGoods.ExpandAll();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            ShowGoodListView();

            if (this.lvwGoods.Items.Count > 0)
            {
                ListViewItem item = this.lvwGoods.Items[0];
                if (item != null)
                {
                    TreeNode node = FindNode(item);
                    this.treeGoods.ExpandAll();
                    this.treeGoods.SelectedNode = node;
                }
            }
        }

        private TreeNode FindNode(ListViewItem item)
        {
            foreach (TreeNode typeNode in this.treeGoods.Nodes)
            {
                foreach (TreeNode node in typeNode.Nodes)
                {
                    if (node.Tag != null)
                    {
                        if (WareHourse == "客房仓库")
                        {
                            GoodsInfo info = item.Tag as GoodsInfo;
                            GoodsInfo goodsInfo = node.Tag as GoodsInfo;
                            if (goodsInfo != null &&
                                goodsInfo.ID == info.ID)
                            {
                                return node;
                            }
                        }
                        else if (WareHourse == "KTV仓库")
                        {
                            KTVGoodsInfo info = item.Tag as KTVGoodsInfo;
                            KTVGoodsInfo goodsInfo = node.Tag as KTVGoodsInfo;
                            if (goodsInfo != null &&
                                goodsInfo.ID == info.ID)
                            {
                                return node;
                            }
                        }
                        else if (WareHourse == "茶室仓库")
                        {
                            TeaGoodsInfo info = item.Tag as TeaGoodsInfo;
                            TeaGoodsInfo goodsInfo = node.Tag as TeaGoodsInfo;
                            if (goodsInfo != null &&
                                goodsInfo.ID == info.ID)
                            {
                                return node;
                            }
                        }
                    }
                }
            }
            return null;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.lvwGoods.SelectedItems.Count > 0)
            {
                ListViewItem item = this.lvwGoods.SelectedItems[0];//按钮默认加第一个
                if (WareHourse == "客房仓库")
                {
                    GoodsInfo info = item.Tag as GoodsInfo;
                    int count = Convert.ToInt32(this.txtQuantity.Text);
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }
                else if (WareHourse == "KTV仓库")
                {
                    KTVGoodsInfo info = item.Tag as KTVGoodsInfo;
                    int count = Convert.ToInt32(this.txtQuantity.Text);
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }
                else if (WareHourse == "茶室仓库")
                {
                    TeaGoodsInfo info = item.Tag as TeaGoodsInfo;
                    int count = Convert.ToInt32(this.txtQuantity.Text);
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }

                BindData();            
            }
        }

        private void lvwGoods_DoubleClick(object sender, EventArgs e)
        {
            foreach(ListViewItem item in this.lvwGoods.SelectedItems)
            {
                if (WareHourse == "客房仓库")
                {
                    GoodsInfo info = item.Tag as GoodsInfo;
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }
                else if (WareHourse == "KTV仓库")
                {
                    KTVGoodsInfo info = item.Tag as KTVGoodsInfo;
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }
                else if (WareHourse == "茶室仓库")
                {
                    TeaGoodsInfo info = item.Tag as TeaGoodsInfo;
                    if (info != null)
                    {
                        InsertOnItem(info);
                    }
                }
            }
            BindData();
        }

        #region 写入到字典列表中

        private void InsertOnItem(GoodsInfo goodsInfo)
        {
            #region 构造消费信息
            int count = Convert.ToInt32(this.txtQuantity.Text);
            PurchaseDetailInfo detailInfo = new PurchaseDetailInfo();
            detailInfo.Amount = goodsInfo.UnitCost * count;
            detailInfo.ItemName = goodsInfo.Name;
            detailInfo.ItemNo = goodsInfo.ItemNo;
            detailInfo.OperationType = "进货";
            detailInfo.Price = goodsInfo.UnitCost;
            detailInfo.Quantity = count;
            detailInfo.Unit = goodsInfo.UnitName;
            detailInfo.DuralDays = goodsInfo.DuralDays;
            detailInfo.ExpireDate = DateTime.Now.AddDays(detailInfo.DuralDays);
            //detailInfo.PurchaseHead_ID = 
            #endregion

            if (detailDict.ContainsKey(goodsInfo.ItemNo))
            {
                PurchaseDetailInfo tempInfo = detailDict[goodsInfo.ItemNo];
                tempInfo.Amount += goodsInfo.UnitCost * count;
                tempInfo.Quantity += count;
            }
            else
            {
                detailDict.Add(goodsInfo.ItemNo, detailInfo);
            }
        }

        private void InsertOnItem(KTVGoodsInfo goodsInfo)
        {
            #region 构造消费信息
            int count = Convert.ToInt32(this.txtQuantity.Text);
            PurchaseDetailInfo detailInfo = new PurchaseDetailInfo();
            detailInfo.Amount = goodsInfo.UnitCost * count;
            detailInfo.ItemName = goodsInfo.Name;
            detailInfo.ItemNo = goodsInfo.ItemNo;
            detailInfo.OperationType = "进货";
            detailInfo.Price = goodsInfo.UnitCost;
            detailInfo.Quantity = count;
            detailInfo.Unit = goodsInfo.UnitName;
            detailInfo.DuralDays = goodsInfo.DuralDays;
            detailInfo.ExpireDate = DateTime.Now.AddDays(detailInfo.DuralDays);
            //detailInfo.PurchaseHead_ID = 
            #endregion

            if (detailDict.ContainsKey(goodsInfo.ItemNo))
            {
                PurchaseDetailInfo tempInfo = detailDict[goodsInfo.ItemNo];
                tempInfo.Amount += goodsInfo.UnitCost * count;
                tempInfo.Quantity += count;
            }
            else
            {
                detailDict.Add(goodsInfo.ItemNo, detailInfo);
            }
        }

        private void InsertOnItem(TeaGoodsInfo goodsInfo)
        {
            #region 构造消费信息
            int count = Convert.ToInt32(this.txtQuantity.Text);
            PurchaseDetailInfo detailInfo = new PurchaseDetailInfo();
            detailInfo.Amount = goodsInfo.UnitCost * count;
            detailInfo.ItemName = goodsInfo.Name;
            detailInfo.ItemNo = goodsInfo.ItemNo;
            detailInfo.OperationType = "进货";
            detailInfo.Price = goodsInfo.UnitCost;
            detailInfo.Quantity = count;
            detailInfo.Unit = goodsInfo.UnitName;
            detailInfo.DuralDays = goodsInfo.DuralDays;
            detailInfo.ExpireDate = DateTime.Now.AddDays(detailInfo.DuralDays);
            //detailInfo.PurchaseHead_ID = 
            #endregion

            if (detailDict.ContainsKey(goodsInfo.ItemNo))
            {
                PurchaseDetailInfo tempInfo = detailDict[goodsInfo.ItemNo];
                tempInfo.Amount += goodsInfo.UnitCost * count;
                tempInfo.Quantity += count;
            }
            else
            {
                detailDict.Add(goodsInfo.ItemNo, detailInfo);
            }
        }
        #endregion

        private void treeGoods_DoubleClick(object sender, EventArgs e)
        {
            btnAdd_Click(null,null);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvwDetail_DoubleClick(object sender, EventArgs e)
        {
            btnDelete_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //消费退单
            if (this.lvwDetail.SelectedItems.Count == 0) return;

            ListViewItem item = this.lvwDetail.SelectedItems[0];
            PurchaseDetailInfo info = item.Tag as PurchaseDetailInfo;
            if (info != null)
            {
                if(detailDict.ContainsKey(info.ItemNo))
                {
                    detailDict.Remove(info.ItemNo);
                }
                BindData();
            }
        }


        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAdd_Click(null, null);
            }
        }

        private void menuAdd_Add_Click(object sender, EventArgs e)
        {
            btnAdd_Click(null, null);
        }

    }
}
